define(["../common"], function() {
	require(["dot"], function() {
		// 长标题自动截断
		$(".dot").dotdotdot({wrap: 'letter'});
    	
	});
	
	$(".navbar-brand-region").click(function() {
		$(".regionModal").modal('show');
	});
	
	Region.visiting(function(region) {
		// 标题栏显示位置信息
		$(".navbar-brand-region").html(region.name);
		
		// 触屏版轮翻广告
		var ads = $.parseJSON(region.ads).mob;
		var ad;
		var $slider = $("#slider");
		var $pagenavi = $("#pagenavi");
		var defImg = g_resDomain + "/v1.5/images/goods-def.png"
		var str = ""
		for (var i in ads) {
			ad = ads[i];
			$slider.append('<li><a href="' + ad.url + '"><img src="' + defImg + '" data-src="' + ad.img + '" /></a></li>');
			$pagenavi.append('<a></a>')
		}
		
		require(["touchslider"], function() {
			$("#swipe").swipe();
			// 触发滚动事件,使图片显示
			$(window).scroll();
		});
		
		// 搜索热词
		var words = $.parseJSON(region.words).mob;
		var word;
		var $hotwords = $("#hotwords");
		for (var i in words) {
			word = words[i];
			$hotwords.append('<a href="' + word.url + '">'+ word.word +'</a>');
		}
		
	});
	
	/**
	 * 最近浏览
	 */
	(function() {
		
		var $latestMy = $("#latest-my");
		var latestMyHtmlTemp = $("<div>").append($latestMy.children("a")).html();
		
		$.getJSON(g_behaviorDomain + "/storeGoodsAccess/latest4ov/json", {size: 6}, function(data) {
			if (data.status !== 0) {
				return;
			}
			var list = data.result;
			var len;
			if (!list || (len = list.length) == 0) {
				return;
			}
			
			$latestMy.show();
			
			var sg; // storeGoods
			var itemTmp;var distance = "";
			for (var i = 0; i < len; i++) {
				sg = list[i].storeGoods;
				
				itemTmp = latestMyHtmlTemp
					.parse("detailUrl", g_domain + "/storeGoods/detail/" + sg.storeGoodsId)
					.parse("pic", sg.pic)
					.parse("title", sg.title)
					.parse("price", sg.price)
					.parse("prom", "")
					.parse("storeUrl", g_domain + "/store/" + sg.store.storeId)///
					.parse("storeName", sg.store.name)
					.parse("storeDistance", "")
					;
				$latestMy.append(itemTmp);
			}
			
			GoodsList.applyLayout();
			
			// 触发滚动事件,使图片显示
			$(window).scroll();
			
		});
		
	})();
	
	/**
	 * 大家在看
	 */
	(function() {
		// 大家在看
		var $latestNear = $("#latest-near");
		var $latestNearList = $latestNear.find(".scroll-list");
		var latestNearHtmlTemp = $("<div>").append($latestNearList.children()).html();
		
		Region.visiting(function(region) {
			latestNear_(region.loc[1], region.loc[0]);
		});
		
		function latestNear_(lat, lng) {		
			
			$.getJSON(g_behaviorDomain + "/storeGoodsAccess/latest4nv/json", {lat: lat, lng: lng, size: 10}, function(data) {
				if (data.status !== 0) {
					return;
				}
				var list = data.result;
				var len;
				if (!list || (len = list.length) == 0) {
					return;
				}
				
				$latestNear.show();
				
				var sg; // storeGoods
				var itemTmp;
				for (var i = 0; i < len; i++) {
					sg = list[i].storeGoods;
					
					itemTmp = latestNearHtmlTemp
						.parse("url", g_domain + "/storeGoods/detail/" + sg.storeGoodsId)
						.parse("pic", sg.pic)
						.parse("title", sg.title)
						.parse("price", sg.price);
					$latestNearList.append(itemTmp);
				}
				
				// 触发滚动事件,使图片显示
				$(window).scroll();
				
				$latestNear.find(".scroll-panel").horizontalScrolling();// horizontalScrolling(3000)
			});
		}
		
	})();
	
	/**
	 * 选择地区
	 */
	(function() {
		var initRegion; // 初始地区
		var selectedRegion;// 当前选中的地区
		var $modalRegionlist = $(".regionModal .modal-body");	
		var regionItemTemp = $modalRegionlist.html();
		$modalRegionlist.empty();	
		var $lockTarget = $(".regionModal");
		
		// 打开地区选择弹出框之前
		$(".regionModal").on('show.bs.modal', function (e) {
			// 清空表单
			$("#rm_searchWord").val("");
			
			$lockTarget.loadMsg("加载地区...");
			Region.visiting(function(region) {
				initRegion = region;
				selectRegion_(region);		
			});
		});
		// 关闭地区选择弹出框之后
		$(".regionModal").on('hidden.bs.modal', function (e) {
			// 地区没变
			if (selectedRegion.regionId == initRegion.regionId) {
				return;
			}
			// 保存设置,并刷新页面
			Region.setVisiting(selectedRegion.regionId, function() {
				// 刷新页面
				location.reload();
			});
			
			return false;
		});
		
		// 返回上一级地区列表
		$("#rm_selected").parent().click(function() {
			$lockTarget.loadMsg("加载地区...");
			$.getJSON(g_behaviorDomain + "/region/one/json", {regionId: selectedRegion.parentId}, function(data) {
				if (data.status !== 0) {
					$modalRegionlist.html("<p>" + data.message + "</p>");
					return;
				}
				var region = data.result;
				if (!region) {
					return;
				}
				selectRegion_(region);
			});
			
		});
		
		// 查找
		$("#rm_btn").click(function() {
			var sw = $("#rm_searchWord").val();
			if (sw) {
				searchResgion_(sw);
			}			
		});
		
		// 所在地区
		Region.locating(function(region) {
			$("#rm_located").html(region.name);
			// 注册事件: 选择所在地区为访问地区
			$("#rm_located").parent().unbind('click').bind('click', (function(region) {
				return function() {
					$lockTarget.loadMsg("加载地区...");
					selectRegion_(region);
				}
			})(region));
		});
		
		function selectRegion_(region) {
			if (region.type >= 1) {
				$("#rm_selected").html(region.name);
				selectedRegion = region;
			}						
			loadSubRegions_(region);
		}
		
		function searchResgion_(searchWord) {
			$lockTarget.loadMsg("查找地区...");
			$.getJSON(g_behaviorDomain + "/region/search/json", {searchWord: searchWord}, function(data) {
				parseSubRegions_(null, data);
			});
		}
		
		function loadSubRegions_(region) {
			var loadPath = g_behaviorDomain + "/region/listByParentId/json";
			var vars = {parentId: region.regionId};			
			var cachekey = $.param({path: loadPath, vars: vars});
			
			// 从缓存获取
			var cacheData = Common.getSessionStorage(cachekey);
			if (cacheData) {
				parseSubRegions_(region, $.parseJSON(cacheData));
				return;
			}
			// 从服务器获取
			$.ajax({
				url: loadPath + "?r=" + Math.random(),
				data: vars,
				dataType: 'text',
				success: function(data, textStatus, jqXHR) {
					Common.setSessionStorage(cachekey, data);
					parseSubRegions_(region, $.parseJSON(data));
				}
			});
			
		}
		
		function parseSubRegions_(region, data) {
			$lockTarget.hideMsg();
			if (data.status !== 0) {
				$modalRegionlist.html("<p>" + data.message + "</p>");
				return;
			}
			var list = data.result;
			var len;
			
						
			if (!list || (len = list.length) == 0) {
				
				if (region) {// 如果已选择的是最底一级,则始终显示上一级地区列表
					
					var parent = new Object();
					parent.regionId = region.parentId;
					loadSubRegions_(parent);
					
				} else {// 查无记录
					
					$modalRegionlist.html("<p>找不到地区</p>");
				}
				return;
			}
			
			$modalRegionlist.empty();
			var subRegion;
			var itemHtml;
			for (var i in list) {
				
				subRegion = list[i];
				itemHtml = regionItemTemp
					.parse("id", subRegion.regionId)
					.parse("name", subRegion.name);
				$modalRegionlist.append(itemHtml);
				
				$("#rm_region_" + subRegion.regionId).unbind('click').bind('click', (function(region) {
					return function() {
						$lockTarget.loadMsg("加载地区...");
						selectRegion_(region);
					}
				})(subRegion));
			}
		}
	})();
	
});